<template>
  <div class="detail-loading" v-if="isLoading">
    <van-loading type="spinner" />
  </div>
  <div class="detail" v-else>
    <div class="header">
      <button @click="back">back</button>
      {{ name }}
    </div>
    <img :src="url" alt="" />
    <hr />
    <!-- 歌手列表 -->
    <ul>
      <li v-for="(item, index) in list" :key="item.id" @click="play(index)">
        <p>{{ item.name }}</p>
        <hr />
        <div>{{ item.album }}</div>
      </li>
    </ul>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
import { getSongList, getSongsUrl } from "./service";
import { handImgUrl } from "../Singer/index.js";
export default {
  data() {
    return {
      isLoading: true,
      name: "暂无数据",
      url: "",
      list: [],
    };
  },
  methods: {
    ...mapMutations(["changeSongList", "changeCurrIdx", "toggleScreen"]),
    play(index) {
      if (this.urls && this.urls.length > 0) {
        // 将歌曲url列表 和  信息列表进行合并
        const arr = this.list.map((item, index) => {
          return { ...item, url: this.urls[index] };
        });
        console.log(arr);
        console.log("开始播放", index);
        // 将当前歌单的列表加入vuex
        this.changeSongList(arr);
        // 当前播放歌曲的下标加入vuex
        this.changeCurrIdx(index);
        // 变成大屏播放器
        this.toggleScreen(true);
      }
    },
    back() {
      this.$router.go(-1);
    },
    async getSongsUrlData(mids) {
      const { err, urls } = (await getSongsUrl(mids)) || {};
      if (err) return false;
      this.urls = urls || [];
    },
    async getSongData(singermid) {
      const { code, result } = (await getSongList(singermid)) || {};
      this.isLoading = false;
      if (code) return false;
      // 根据mid 获取歌曲的url
      let mids = result?.songs.map((item) => item.mid);
      this.getSongsUrlData(mids);
      this.list = result?.songs || [];
    },
  },
  created() {
    this.name = this.$route.query.name;
    this.url = handImgUrl(this.$route.params.id);
    // console.log(this.$route.params.id);
    this.getSongData(this.$route.params.id);
  },
};
</script>
<style lang="less" scoped>
@import "@styles/index.less";
.detail {
  overflow: scroll;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: @bg-black;
  z-index: 3;
  .header {
    .h(44);
    .w(375);
  }
  img {
    .w(375);
    .h(340);
  }
  li {
    .h(80);
    font-size: 18px;
  }
}
.detail-loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  background: @bg-black;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
